/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import Exceptions.NonexistentEntityException;
import entitys.Veterinarian;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author EdwinHernan
 */
public class VeterinarianService implements Serializable {
    public VeterinarianService(EntityManagerFactory emf){
        this.emf=emf;
    }
    private EntityManagerFactory emf=null;
    
    public EntityManager getEntityManager(){
        return emf.createEntityManager();
    }
    
     /**
     *
     * @param object
     */
    
    public void create(Veterinarian veterinarian){
        EntityManager em=null;
        try{
            em=getEntityManager();
            em.getTransaction().begin();
            em.persist(veterinarian);
            em.getTransaction().commit();
        }finally{
            if(em!=null){
                em.close();
            }
        }
    }
    public void edit(Veterinarian veterinarian) throws NonexistentEntityException, Exception {
        EntityManager em=null;
        try{
            em=getEntityManager();
            em.getTransaction().begin();
            em.merge(veterinarian);
            em.getTransaction().commit();  
        }catch(Exception ex){
            String msg = ex.getLocalizedMessage();
            if(msg==null || msg.length()==0){
                Long id=veterinarian.getIdPerson();
                //if(findPerson(id)==null){
                    //throw new NonexistentEntityException("The person with id" +id+"no longer exists.");
                //}
            }
            throw ex;
        }finally{
            if(em !=null){
                em.close();
            }
        }
        
    }
    public void destroy(Long id) throws NonexistentEntityException{
        EntityManager em=null;
        try{
            em=getEntityManager();
            em.getTransaction().begin();
            Veterinarian veterinarian;
            try{
                veterinarian=em.getReference(Veterinarian.class, id);
                veterinarian.getIdPerson();
            }catch(EntityNotFoundException enfe){
                throw new NonexistentEntityException("the person with id "+id+"no longer exist.",enfe);
            }
            em.remove(veterinarian);
            em.getTransaction().commit();
        }finally{
            if(em!=null){
                em.close();
            }
        }
    }
    public List<Veterinarian> findVeterinarianEntities(){
        return findVeterinarianEntities(true,-1,-1);
    }
    public List<Veterinarian> findVeterinarianEntities(int maxResults, int firstResult){
        return findVeterinarianEntities(false,maxResults,firstResult);
    }
    private List<Veterinarian>findVeterinarianEntities(boolean all,int maxResults,int firstResult){
        EntityManager em=getEntityManager();
        try{
            CriteriaQuery cq=em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Veterinarian.class));
            Query q=em.createQuery(cq);
            if((!all)){
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }           
            return q.getResultList();
        }finally{
            em.close();
        }
    }
}
